package com.vmix.vmixmobiledemo;

import com.vmix.vmixmobiledemo.VmixCollectionListAdapter;
import com.vmix.vmixmobiledemo.BrowseVideosActivity;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.util.Log;
import android.view.View;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.IBinder;

// import vmixsdk collections service
import com.vmix.android.vmixsdk.rds.GetCollectionsService;
import com.vmix.android.vmixsdk.valueobject.VmixCollections;

public class CollectionsActivity extends ListActivity {
	
    /** Called when the activity is first created. */
	
	ListView list;
	VmixCollectionListAdapter adapter;
	// private vmix collections
	private VmixCollections collections;
	// intent filter for the getCollections message
	private IntentFilter filter = null;
	// intent receiver
	private GetCollectionsReceiver receiver = null;
	// service reference
	private GetCollectionsService serviceBinder = null;
	
    /**
     * Handle the connections between the service and activity
     */
    private ServiceConnection serviceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName className) {
			// TODO Auto-generated method stub
			serviceBinder = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			
			serviceBinder = (( GetCollectionsService.GetCollectionsBinder) service).getService();
		
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collections);
        
        // register intent filter for this class
        this.filter = new IntentFilter( GetCollectionsService.COLLECTIONS_FOUND );
        
        this.receiver = new GetCollectionsReceiver();
        registerReceiver( this.receiver, this.filter);
        
        // start the service
        this.startGetCollectionsService();
    }
      
    
    @Override
    public void onDestroy()
    {
    	super.onDestroy();

		// unregister receiver
		unregisterReceiver(this.receiver);
		
		// unbind service connection and stop service explicitly
		this.stopGetCollectionsService();
    }
    
    /**
     * Add broadcast receiver to intercept the complication of loading getCollections
     * 
     * @author lei
     *
     */
    public class GetCollectionsReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			// use the binding to pull the collections
			
			startMainActivity();
			
		}
    	
    }
    
    /**
     * start the getCollections service
     */
    private void startGetCollectionsService() {

    	startService( new Intent( this, GetCollectionsService.class ));
    
    	// bind the service as well
    	Intent bindIntent = new Intent( CollectionsActivity.this, GetCollectionsService.class);
    	getApplicationContext().bindService( bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
    	
    }
    
    // stop service
    private void stopGetCollectionsService() {
    	
    	// unbind
    	getApplicationContext().unbindService( this.serviceConnection );
    	
    	// stop service
    	stopService( new Intent(this, GetCollectionsService.class));
    }
    
	/**
	 * move the activity starting methods into this private function
	 */
	private void startMainActivity() {

		// use the binding to execute service public function
		collections = serviceBinder.getCollections();
		
		list = (ListView) findViewById(android.R.id.list);
		adapter = new VmixCollectionListAdapter(this, collections);

		list.setAdapter(adapter);
		list.setTextFilterEnabled(true);

		list.setOnItemClickListener(new OnItemClickListener() {

			// @Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent i = new Intent(CollectionsActivity.this,
						VideosTabActivity.class);
				
				// set the collectionId across to the next activity
				String collectionId = collections.get( position ).getCollectionId();
				// set the collection id on the intent
				i.putExtra("collectionId", collectionId);
				
				startActivity(i);
			}

		});
	}
	
}